<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%article}}".
 *
 * @property integer $id
 * @property string $title
 * @property integer $cat
 * @property string $keyword
 * @property string $info
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $online
 */
class Article extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%article}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title','cat'], 'required'],
            [['cat','created_at','updated_at','online'], 'integer'],
            [['title', 'keyword'], 'string', 'max' => 255],
            [['info'], 'string', 'max' => 800],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'title' => Yii::t('app', '文章标题'),
            'cat' => Yii::t('app', '文章分类'),
            'keyword' => Yii::t('app', '关键词'),
            'info' => Yii::t('app', '描述'),
            'created_at' => Yii::t('app', '创建'),
            'updated_at' => Yii::t('app', '更新'),
            'online' => Yii::t('app', '是否在线浏览'),
        ];
    }
    public function beforeSave($insert)
    {
        $time  = time();
        if($this->isNewRecord){
            $this->created_at = $time;
            $this->updated_at = $time;
        }else{
            $this->updated_at = $time;
        }
        if(empty($this->cat)){
            $this->cat = 0;
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    public function getTxt(){
        return $this->hasOne(ArticleTxt::className(),['id'=>'id']);
    }

    public function getCats(){
        return $this->hasOne(Category::className(),['id'=>'cat']);
    }
}
